package com.ldc.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ldc.model.SysUser;
import com.ldc.service.mapper.UserMapper;
import com.ldc.service.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能说明
 *
 * @auther liudc
 * @since 2023/6/11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {

    @Override
    public SysUser findByUserName(String userName) {
        return findBy(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName, userName));
    }

    @Override
    public SysUser findByUserId(String userId) {
        return findBy(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, userId));
    }

    @Override
    public SysUser findBy(LambdaQueryWrapper<SysUser> lambdaQueryWrapper) {
        return getBaseMapper().selectOne(lambdaQueryWrapper);
    }

    @Override
    public int deleteByUserId(String userId) {
        return deleteBy(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, userId));
    }

    @Override
    public int deleteBy(LambdaQueryWrapper<SysUser> queryWrapper) {
        return getBaseMapper().delete(queryWrapper);
    }

    @Override
    public IPage<SysUser> findPageBy(IPage<SysUser> page, LambdaQueryWrapper<SysUser> queryWrapper) {
        return getBaseMapper().selectPage(page, queryWrapper);
    }

    @Override
    public IPage<SysUser> findPageByPageQuery(long page, long offset, String query) {
        return findPageBy(Page.of(page, offset, true), new LambdaQueryWrapper<SysUser>()
                .like(SysUser::getUserName, query)
                .or()
                .eq(SysUser::getId, query));
    }

    @Override
    public List<SysUser> findListBy(LambdaQueryWrapper<SysUser> queryWrapper) {
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<SysUser> findListByIds(List<Long> ids) {
        return findListBy(new LambdaQueryWrapper<SysUser>().in(SysUser::getId, ids));
    }

    @Override
    public List<Long> getUserIds(List<Long> ids) {
        return findListByIds(ids).stream().map(SysUser::getId).collect(Collectors.toList());
    }

    @Override
    public int batchDeleteBy(LambdaQueryWrapper<SysUser> queryWrapper) {
        return getBaseMapper().delete(queryWrapper);
    }

    @Override
    public int batchDeleteByIds(List<Long> ids) {
        return batchDeleteBy(new LambdaQueryWrapper<SysUser>().in(SysUser::getId, ids));
    }
}
